#include "replycomment.h"
#include "ui_replycomment.h"

replyComment::replyComment(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::replyComment)
{
    ui->setupUi(this);
    db = QSqlDatabase::database("conn");//链接数据库
    ui->confirmBtn->setShortcut(Qt::Key_Return);//设置保存快捷键为键盘的“回车”键
}

replyComment::~replyComment()
{
    delete ui;
}

void replyComment::set(int id,QString customer,QString comment,QString extra)
{
    this->id = id;
    this->extra = extra;
    ui->commentEdit->append(customer + ":" + comment);
    QStringList list = extra.split("&",QString::SkipEmptyParts);
    foreach(QString content,list){
        ui->extraEdit->append(content);
    }
}

void replyComment::on_confirmBtn_clicked()
{
    QSqlQuery query;//创建对象时，系统会自动完成跟数据库的关联
    query.setForwardOnly(true);//减少内存浪费

    QString reply = ui->replyEdit->text();

    if(reply.isEmpty()){
        QMessageBox::information(this, "友情提示", "请输入回复内容");
        return;
    }

    QString currentExtra = extra + "&我：" + reply;
    QString sqlUpdate = QString("update comment set extra = '%1' where id = %2").arg(currentExtra).arg(id);
    //    UPDATE table_name
    //    SET column1 = value1, column2 = value2...., columnN = valueN
    //    WHERE [condition];
    if(!query.exec(sqlUpdate)){
        qDebug() << "update data error" << db.lastError();
    }else{
        ui->extraEdit->append("我：" + reply);
        ui->replyEdit->clear();
    }
}
